<?php
/*  
  Copyright (c) 2010-02 SongCMS 
  SongCMS All Rights Reserved. 
  Support:www.SongCMS.com
  QQ:544255858   19951025
  Author:Song  Version:3.17
  Date:2010-08-12 09:28:32
*/
class article extends db{
	public $isSelect = NULL; 
	public $Ltext = array(); 
	public $Lang = 1;
	public $nColor = '#FF0000'; 
	public $xColor = '#666666'; 
	public $HTMLDir = "./html/"; 
	public $SelectColor = '#FF0000'; 
	private $result,$SQL;
	public function sList($params){
		global $SC,$isDynamicURL;
		extract($params);
		!is_numeric($Sort) ? $Sort = 0 : NULL; empty($Rela) ? $Rela = 0 : NULL;
		!is_numeric($Start) ? $Start = 0 : NULL; !is_numeric($Total) ? $Total = 10 : NULL;
		!is_numeric($isImg) ? $isImg = 0 : NULL; empty($BackVar) ? $BackVar = 'article' : NULL;
		$isPage = !is_numeric($isPage) ? 0 : $isPage;
		if(!is_numeric($Interception)) $Interception = 0;
		if(empty($Symbol)) $Symbol = '...';		
		$SQL = "SELECT * FROM `{$this->dbprefix}article` WHERE `ArticleLang` = {$this->Lang} && `ArticleAudit` = 1 ";
		if($Rela!=0) $SQL .= "&& `ArticleRela` LIKE '%" . $Rela . "%' "; 
		if(!empty($this -> isSelect) && $isSearch == 1) $SQL .= "&& (`ArticleTitle` LIKE '%" . $this -> isSelect . "%' || `ArticleKeyWord` LIKE '%" . $this -> isSelect . "%' || `ArticleContent` LIKE '%" . $this -> isSelect . "%') "; 
		if($isImg==1) $SQL .= "&& `ArticleImg` <> '' "; elseif ($isImg==2) $SQL .= "&& `ArticleImg` == '' ";
		switch ($Sort){
			case 1:
				$SQL .= "ORDER BY `ID` DESC,`ArticleSort` DESC "; break;
			case 2:
				$SQL .= "ORDER BY `ArticleClick` DESC,`ID` DESC "; break;
			case 3:
				$SQL .= "ORDER BY `ArticleRecommended` DESC ,`ArticleSort` DESC,`ID` DESC "; break;
			case 4:
				$SQL .= "ORDER BY `ID` ASC "; break;
			default:
				$SQL .= "ORDER BY `ArticleSort` DESC,`ID` DESC "; break;
		}
		if ($isPage == 0){
			$SQL .= "LIMIT {$Start},{$Total}";
		}else{
			global $sGET;
			$page = !is_numeric($sGET['p']) ? 1 : $sGET['p']; 
			$RsCount = count($this->SelectSQL($SQL)); 
			$TotalPages = ceil($RsCount / $Total); 
			if($page>$TotalPages){$page = $TotalPages;}
			if($page<1){$page=1;}
			if($isDynamicURL == 1 || !empty($sGET['w'])){ 
				$url = $this -> url();
				$xURL = "./{$url}1"; 
				$oURL = "./" . $url . ($page-1); 
				$sURL = "./" . $url . ($page+1); 
				$nURL = "./" . $url . $TotalPages; 
			} else {
				$kCid = $sGET['c'];
				$kCid = explode('|',$kCid);
				$kCid = end($kCid);
				if($TotalPages > 1){
					global $HTML_PAGE_CONFIG;
					$htmlpage_file = './config/htmlpage.php'; 
					include_once($htmlpage_file);
					$oRepeat = false; 
					if(!is_array($HTML_PAGE_CONFIG['page'])) $HTML_PAGE_CONFIG['page'] = array();
					foreach($HTML_PAGE_CONFIG['page'] as $i => $n){
						if($n[0] == $kCid){
							$oRepeat = true;
						}
					}					
					if($oRepeat == false){
						$HTML_PAGE_CONFIG['page'][] = array($kCid,$TotalPages);
						$oxText = "<?php \r\n";
						$oxText .= '$HTML_PAGE_CONFIG = ' . var_export($HTML_PAGE_CONFIG,true) . "\r\n";
						$oxText .= '?>';						
						if(!file_put_contents($htmlpage_file,$oxText)){
							die('无法写入分页配置信息：'.$htmlpage_file);	
						}
					}
					unset($oxText);
				}
				$xURL = "./html/page/{$kCid}.html"; 
				$oURL = $page > 2 ? "./html/page/{$kCid}" . ($page-1) . '.html' : $xURL; 
				$sURL = "./html/page/{$kCid}" . ($page+1) . '.html'; 
				$nURL = "./html/page/{$kCid}{$TotalPages}.html"; 
			}
			$pages = "{$this->Ltext[0]}:<font color=\"{$this->nColor}\">{$RsCount}</font>&nbsp;|&nbsp; \n";
			if($page <= 1){
				$pages .= "<font color=\"{$this->xColor}\">{$this->Ltext[1]}</font>&nbsp;|&nbsp; \n";
				$pages .= "<font color=\"{$this->xColor}\">{$this->Ltext[2]}</font>&nbsp;|&nbsp; \n";
			} else {
				$pages .= "<a href=\"{$xURL}\">{$this->Ltext[1]}</a>&nbsp;|&nbsp; \n";
				$pages .= "<a href=\"" . $oURL . "\">{$this->Ltext[2]}</a>&nbsp;|&nbsp; \n";
			}
			if($page >= $TotalPages){
				$pages .= "<font color=\"{$this->xColor}\">{$this->Ltext[3]}</font>&nbsp;|&nbsp; \n";
				$pages .= "<font color=\"{$this->xColor}\">{$this->Ltext[4]}</font>&nbsp;|&nbsp; \n";				
			} else {
				$pages .= "<a href=\"" . $sURL . "\">{$this->Ltext[3]}</a>&nbsp;|&nbsp; \n";
				$pages .= "<a href=\"{$nURL}\">{$this->Ltext[4]}</a>&nbsp;|&nbsp; \n";				
			}
			$pages .= "<font color=\"{$this->nColor}\">{$Total}</font>{$this->Ltext[5]}&nbsp;|&nbsp; \n";
			$pages .= '<select name="spage" OnChange="window.open(this.options[this.selectedIndex].value,\'_self\')">' . "\n";
			for ($i=1;$i<=$TotalPages;$i++){
				if($isDynamicURL == 1 || !empty($sGET['w'])){
					$ipturl = './'.$url.$i;
				} else {
					if($i==1){
						$ipturl = $this -> Lang == 1 ? './html/page/index.html' : './html/page/'.$kCid.'.html';
					} else {
						$ipturl = './html/page/'.$kCid.$i.'.html';
					}
				}					
				if ($i!=$page){
					$pages .= '<option value="' .$ipturl . '">' . str_replace('{&i}',$i,$this->Ltext[6]) . '</option>' . "\n";
				}else{
					$pages .= '<option selected value="' . $ipturl . '">' . str_replace('{&i}',$i,$this->Ltext[6]) . '</option>' . "\n";
				}
			}
			$pages .= '</select>' . "\n";	
			$pages .= str_replace('{&TotalPages}',"<font color=\"{$this->nColor}\">{$TotalPages}</font>",$this->Ltext[7]) . " \n";
			$p[0] = $pages; 
			$p[1] = $RsCount; 
			$p[2] = $Total; 
			$p[3] = $page; 
			$p[4] = $TotalPages; 
			$p[5] = $xURL; 
			$p[6] = $oURL; 
			$p[7] = $sURL; 
			$p[8] = $nURL; 
			$SC -> assign('page',$p);
			$SQL .= "LIMIT " . ($page-1)*$Total . " , " . $Total . " ";
			unset($pages,$RsCount,$TotalPages,$page,$url);	
		}
		$result = $this -> SelectSQL($SQL,1);
		foreach ($result as $i => &$n){
			if($Interception > 0) $n['ArticleTitle'] = FSubstr($n['ArticleTitle'],0,$Interception,$Symbol,'utf-8');
			if($isSearch == 1) $n['ArticleTitle'] = str_replace($this -> isSelect,'<span style="font-weight:bold; color:'. $this -> SelectColor .';">' . $this -> isSelect . '</span>',$n['ArticleTitle']); 
			$pTime = strtotime($n['ArticleTime']);
			$n['Difference'] = round((time() - $pTime)/3600); 
			if($isDynamicURL == 1){
				$xLang = $this->Lang == 1 ? NULL : '/l'.$this->Lang;
				$n['URL'] = "./?fpage{$xLang}/i{$n['ID']}";
			} else {
				$n['URL'] = './html/page/' . date("Ym",strtotime($n['ArticleTime'])) . '/' . $n['ID'] . '.html';	
			}
			if(!is_file($n['ArticleImg'])) $n['ArticleImg'] = './images/noimg.gif';
			if(!empty($this -> isSelect) && $isSearch == 1){
				if(stristr($n['ArticleTitle'],$this -> isSelect)) { 
					$n['SEARCHSORT'] = 1;
				} elseif (stristr($n['ArticleTitle'],$this -> isSelect)){ 
					$n['SEARCHSORT'] = 2; 
				} else { 
					$n['SEARCHSORT'] = 3;
				}
				$mp[$i] = $n['SEARCHSORT'];
				$sp[$i] = $n['ID'];
			}
		}
		if(!empty($this -> isSelect) && $isSearch == 1) @array_multisort($mp,SORT_ASC,$sp,SORT_DESC,$result); 
		$SC -> assign($BackVar,$result);
		unset($SQL,$params,$result);
	}
	private function url(){
		global $sGET;
		$sUrl = '?';
		foreach ($sGET as $iname => &$n){
			if ($iname != 'p' && $iname != 'i'){
				if($iname != 'c' && $iname != 'd') $n = rawurlencode($n); 
				$sUrl .= $iname."{$n}/";
			}
		}
		$sUrl .= 'p';
		return $sUrl;
	}	
	public function TemFile($Cid=NULL,$ID=NULL){
		if(empty($ID) && empty($Cid)) return false;
		if(is_numeric($ID)){
			$SQL = "SELECT `ArticleRela` FROM `{$this->dbprefix}article` WHERE `ID` = '{$ID}' LIMIT 0,1";
			$result = $this -> SelectSQL($SQL,0);
			$result = $result[0][0];
			if (!empty($result)) $Cid = $result;
		}
		$Cid = explode('|',$Cid);
		$Cid = $Cid[0];	
		if(!empty($Cid)){
			$SQL = "SELECT `ColumnTemPage`,`ColumnTemDetail` FROM `{$this->dbprefix}column` ";
			$SQL .= "WHERE `ColumnRela` LIKE '%{$Cid}%' && `ColumnLevel` = 1 ";
			$SQL .= "ORDER BY `ColumnRela` ASC LIMIT 0,1";
			$result = $this -> SelectSQL($SQL,0);
			$result = array_shift($result);
		}
		if(empty($result)) $result = array('page.html','page_detail.html');
		$result[2] = $Cid;
		return $result;
	}
}
?>